<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\services\message;

use app\dao\message\SystemMessageDao;
use app\services\BaseServices;
use think\exception\ValidateException;

/**
 * Class：站内信
 * 创建人：Jack
 * 创建时间：2023/11/30 15:24
 * @package app\services\message
 * @mixin SystemMessageDao
 */
class SystemMessageServices extends BaseServices
{
    /**
     * SystemMessageServices constructor.
     * @param SystemMessageDao $dao
     */
    public function __construct(SystemMessageDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 注释：移动端消息列表
     * 创建人：Jack
     * 创建时间：2023/11/30 15:25
     * @param $uid
     * @return array
     */
    public function getMessageSystemList($uid)
    {
        [$page, $limit] = $this->getPageValue();
        $where['is_delete'] = 0;
        $where['uid'] = $uid;
        $list = $this->dao->getMessageList($where, '*', $page, $limit);
        $count = $this->dao->getCount($where);
        if (!$list) return ['list' => [], 'count' => 0];
        foreach ($list as &$item) {
            $item['create_time'] = time_tran($item['create_time']);
        }
        $message = $this->dao->count(['uid' => $uid, 'is_look' => 0, 'is_delete' => 0]);
        return ['list' => $list, 'count' => $count, 'service_num' => $message];
    }

    /**
     * 注释：消息详情
     * 创建人：Jack
     * 创建时间：2023/11/30 15:25
     * @param int $id
     * @param int $uid
     * @return array
     */
    public function getInfo(int $id, int $uid = 0)
    {
        $info = $this->dao->getOne(['id' => $id]);
        if (!$info || $info['is_delete'] == 1) {
            throw new ValidateException('数据不存在!');
        }
        if ($uid && $info['uid'] != $uid) {
            throw new ValidateException('数据不存在!');
        }
        $info = $info->toArray();
        if ($info['is_look'] == 0) {
            $this->update($info['id'], ['is_look' => 1]);
        }
        $info['create_time'] = time_tran($info['create_time']);
        return $info;
    }

    /**
     * 注释：站内信发放
     * 创建人：Jack
     * 创建时间：2023/11/30 15:26
     * @param int   $uid
     * @param array $noticeInfo
     * @return \hdphp\basic\BaseModel|\think\Model
     */
    public function systemSend(int $uid, array $noticeInfo)
    {
        $data = [];
        $data['mark'] = $noticeInfo['mark'];
        $data['uid'] = $uid;
        $data['title'] = $noticeInfo['title'];
        $data['content'] = $noticeInfo['content'];
        $data['type'] = 1;
        $data['create_time'] = time();
        return $this->dao->save($data);
    }

    /**
     * 注释：一键已读
     * 创建人：Jack
     * 创建时间：2023/11/30 15:26
     * @param int $uid
     * @return true
     */
    public function setAllRead(int $uid)
    {
        if (!$this->dao->count(['uid' => $uid, 'is_delete' => 0, 'is_look' => 0])) {
            throw new ValidateException('暂无未读消息!');
        }
        $this->dao->update(['uid' => $uid], ['is_look' => 1]);
        return true;
    }

    /**
     * 注释：删除消息
     * 创建人：Jack
     * 创建时间：2023/11/30 15:27
     * @param int   $uid
     * @param array $ids
     * @return true
     */
    public function setBatchDel(int $uid, array $ids = [])
    {
        if (!$this->dao->count(['uid' => $uid, 'is_delete' => 0])) {
            throw new ValidateException('暂无消息!');
        }
        $where = ['uid' => $uid];
        if ($ids) {
            // 单个删除验证数据
            if (count($ids) == 1) $this->getInfo((int)$ids[0], $uid);
            $where['id'] = $ids;
        }
        $this->dao->update($where, ['is_delete' => 1]);
        return true;
    }


}
